package com.youxue.teachin.set.child.my;

import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import com.ali.music.multiimageselector.MultiImageSelectorUtils;
import com.library.uiframework.fragment.ptr.PagingRecyclerViewFragment;
import com.library.uiframework.mvp.PagingDataLoadPresenter;
import com.library.uiframework.utils.ListUtils;
import com.library.uiframework.utils.MToast;
import com.library.xtype.IAdapter;
import com.library.xtype.NuwaRecyclerViewAdapter;
import com.youxue.teachin.R;
import com.youxue.teachin.common.upload.UploadExeption;
import com.youxue.teachin.common.upload.UploadManager;
import com.youxue.teachin.common.utils.DefaultSubscriber;
import com.youxue.teachin.common.vo.UserVO;
import com.youxue.teachin.set.main.binder.KeyValueBinder;
import com.youxue.teachin.set.main.binder.UserIConBinder;
import com.youxue.teachin.set.main.vo.UserIconVO;
import com.youxue.teachin.set.repository.po.UserPO;
import com.youxue.teachin.user.userSystem.UserSystem;
import me.nereo.multi_image_selector.MultiContent;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * 我的信息
 *
 * @author: zhiwei
 * @date: 2017-10-05 21:32
 * @version: 9.1.0
 */
public class MyDetailFragment extends PagingRecyclerViewFragment implements OnClickListener {
    UserIconVO mUserIconVO;
    NuwaRecyclerViewAdapter mAdapter;
    UserChangePresener mUserChangePresener;

    @Override
    protected IAdapter onCreateAdapter() {
        mAdapter = new NuwaRecyclerViewAdapter();
        mAdapter.register(KeyValueBinder.class);
        mAdapter.register(UserIConBinder.class);
        mAdapter.setOnClickListener(this);
        return mAdapter;
    }

    @Override
    public void onResume() {
        super.onResume();
        onRetryRequested();
    }

    @Override
    public boolean onInterceptLoadMore() {
        return true;
    }

    @Override
    public boolean onInterceptRefresh() {
        return true;
    }

    @Override
    protected PagingDataLoadPresenter onCreatePresenter() {
        return new MyDetailPresenter(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.icon_edit) {
            mUserIconVO = (UserIconVO)v.getTag();
            selectPic();
        }
    }

    private void selectPic() {
        MultiImageSelectorUtils.selectorCrop(this, true);
    }

    String mAreaUri;

    private void uploadImage(String filePath) {
        showDilog();
        UploadManager.getInstance().uploadImageRX(filePath)
            .subscribeOn(Schedulers.io())
            .flatMap(new Func1<String, Observable<String>>() {
                @Override
                public Observable<String> call(final String s) {
                    UserPO userPO = new UserPO();
                    userPO.avatar = s;
                    return UserChangePresener.uploadUser(userPO)
                        .map(new Func1<Void, String>() {
                            @Override
                            public String call(Void aVoid) {
                                return s;
                            }
                        });
                }
            })
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new DefaultSubscriber<String>() {
                @Override
                public void onNext(String s) {
                    super.onNext(s);
                    UserVO userVO = UserSystem.getInstance().getUserVO();
                    userVO.avatar = s;
                    onRetryRequested();
                    hideDilog();
                }

                @Override
                public void onError(Throwable e) {
                    hideDilog();
                    if (e instanceof UploadExeption) {
                        MToast.getInstance().setText(e.getMessage());
                        return;
                    }
                    super.onError(e);

                }
            });

    }

    ProgressDialog progressDialog;

     void showDilog() {
        progressDialog = new ProgressDialog(getActivity());
        progressDialog.setMessage("上传图片zhong...");
        progressDialog.setCancelable(true);
        progressDialog.show();  //将进度条显示出来
    }

    void hideDilog() {
        if (progressDialog != null) {
            progressDialog.dismiss();
            progressDialog = null;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == MultiContent.REQUESTF_SELECTOR_CODE) {
            if (resultCode == Activity.RESULT_OK) {
                List<String> list = data.getStringArrayListExtra(MultiContent.EXTRA_RESULT);
                if (ListUtils.isNotEmpty(list)) {
                    String file = list.get(0);
                    mUserIconVO.mIcon = file;
                    uploadImage(file);
                    mAdapter.notifyItemChanged(0);
                }

            }

        }
    }

}
